@COFE ?= {}
mime = COFE.MIMETypes
utils = COFE.utils

lowercaseFirstLetter = (str) ->
  str[0].toLowerCase() + str[1..str.length-1]  


# CLASS - Represents a COFEWS's Link, which contains: 
# mediatype - type of stream to be returned when called.
# rel - relation to the current object (e.g. self, download)
# url - relative path of the url.
class Link  
  constructor : (mediatype, rel, uri, param)->
    @mediatype = mediatype
    @rel = lowercaseFirstLetter(rel)
    @url = uri    
    @param = param
    switch @rel
      when "folder" then @header = utils.extractHeaderFilename(@param)
      when "search" then @header = param
      else @header = @url

  # Create a link from a <link> xml element.
  Link.fromJson = (data) ->
    new Link(data.mediatype, data.rel, data.uri)
  
  # Create a link from a folder path (e.g. {cofe}\obj)
  Link.fromPath = (folderPath) ->
    url = "/lookup?path=" + folderPath + "&rel=list&json=true"
    new Link(mime.EntryList, "folder", url, folderPath)
   
  # Create a link from search parameter (e.g. root:{cofe}\obj subdir:true filetype:dll)
  Link.fromSearch = (searchParam) ->
    url = "/search?json=true&filter=" + searchParam
    new Link(mime.EntryList, "search", url, searchParam)    

  Link.extractLinkpath = (path) ->  
    return undefined if path.rel isnt "folder"
    return undefined if path.param is undefined || path.param is ""
    return Link.fromPath(utils.extractFilepath(path.param))

COFE.Link = Link